package action;

import java.util.Calendar;
import java.util.Date;

import web.action.Action;

import agent.UserAgent;


@SuppressWarnings("serial")
public class RegisterAction extends Action {

	private static final int OK = 0;

	private String username = null;
	private String password = null;
	private String email = null;
	private String birthDate = null;
	private String birthMonth = null;
	private String birthYear = null;

	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Date getBirth() {
		try {
			int year = Integer.parseInt(birthYear);
			int month = Integer.parseInt(birthMonth) - 1;
			int date = Integer.parseInt(birthDate);
			Calendar birth = Calendar.getInstance();
			birth.set(year, month, date);
			return birth.getTime();
		} catch (Exception e) {
			return null;
		}
	}
	public void setBirthDate(String birthDate) {
		this.birthDate = birthDate;
	}
	public void setBirthMonth(String birthMonth) {
		this.birthMonth = birthMonth;
	}
	public void setBirthYear(String birthYear) {
		this.birthYear = birthYear;
	}

	public String execute() throws Exception {
		log.info("Register Action Called");
		int result = UserAgent.getSole().registerUser(getUsername(), getPassword(), getEmail(), getBirth());
		switch (result) {
			case OK: {
				setLoggedUser(username);
				return SUCCESS;
			}
			default: {
				addActionError(getText("Register.invalidRegister"));
				return ERROR;
			}
		}
	}
}
